<script setup lang="ts" name="ConsultItem">
import { OrderType } from '@/enums'
import { ref } from 'vue'
import type { ConsultOrderItem } from '@/types/consult'
import {
  showConfirmDialog,
  showFailToast,
  showLoadingToast,
  showSuccessToast,
} from 'vant'
import { cancelOrder, deleteOrder } from '@/services/consult'
import {
  useCancelOrder,
  useDeleteOrder,
  useShowPrescription,
} from '@/composable'

const { onShowPrescription } = useShowPrescription()

const emit = defineEmits<{
  (eventName: 'on-del', id: string): void
}>()

defineProps<{ item: ConsultOrderItem }>()

const { loading: loadingOfCancel, cancelConsultOrder: onCancelConsultOrder } =
  useCancelOrder()

// // #region 取消订单
// // 用来记录是否正在取消中...
// const loadingOfCancel = ref(false)

// const onCancelConsultOrder = async (item: ConsultOrderItem) => {
//   await showConfirmDialog({
//     title: '温馨提示',
//     message: '你确定要取消问诊吗？',
//   })

//   loadingOfCancel.value = true
//   // 1. 调用api接口
//   try {
//     await cancelOrder(item.id)
//     // 2. 修改当前操作的记录的状态
//     item.status = OrderType.ConsultCancel
//     item.statusValue = '已取消'
//     showSuccessToast('取消成功')
//   } catch (err) {
//     showFailToast('取消失败，请稍后重试！')
//   } finally {
//     loadingOfCancel.value = false
//   }
// }
// // #endregion

const { loading: loadingOfDel, deleteConsultOrder: delOrder } = useDeleteOrder(
  (item) => {
    emit('on-del', item.id)
  }
)

// // #region 删除订单
// const delOrder = async (item: ConsultOrderItem) => {
//   await showConfirmDialog({
//     title: '温馨提示',
//     message: '你确定要删除吗？',
//   })

//   // vant 同时只能存在一个 toast，后面弹出来的 toast 会自动关闭前面的 toast
//   showLoadingToast({
//     message: '订单删除中...',
//     forbidClick: true,
//     duration: 0 // 不要自动关闭，默认2000ms后自动关闭
//   })

//   try {
//     // 1. 调API
//     await deleteOrder(item.id)
//     // 2. 通知父组件更新 list
//     emit('on-del', item.id)
//     showSuccessToast('删除成功')
//   } catch (err) {
//     showFailToast('删除失败，请稍后重试！')
//   }
// }
// // #endregion
</script>

<template>
  <div class="consult-item">
    <div class="head van-hairline--bottom">
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p>{{ item.docInfo?.name || '暂未分配医生' }}</p>
      <span
        :class="{
          orange: item.status === OrderType.ConsultPay,
          green: item.status === OrderType.ConsultChat,
        }"
        >{{ item.statusValue }}</span
      >
    </div>
    <div class="body" @click="$router.push(`/user/consult/${item.id}`)">
      <div class="body-row">
        <div class="body-label">病情描述</div>
        <div class="body-value">{{ item.illnessDesc }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">价格</div>
        <div class="body-value">¥ {{ item.payment.toFixed(2) }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">创建时间</div>
        <div class="body-value tip">{{ item.createTime }}</div>
      </div>
    </div>
    <div class="foot" v-if="item.status === OrderType.ConsultPay">
      <van-button
        class="gray"
        plain
        size="small"
        round
        @click="onCancelConsultOrder(item)"
        :loading="loadingOfCancel"
        >取消问诊</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/user/consult/${item.id}`"
      >
        去支付
      </van-button>
    </div>
    <div class="foot" v-if="item.status === OrderType.ConsultWait">
      <van-button
        class="gray"
        plain
        size="small"
        round
        @click="onCancelConsultOrder(item)"
        :loading="loadingOfCancel"
        >取消问诊</van-button
      >
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
      >
        继续沟通
      </van-button>
    </div>
    <div class="foot" v-if="item.status === OrderType.ConsultChat">
      <van-button
        v-if="item.prescriptionId"
        class="gray"
        plain
        size="small"
        round
        @click="onShowPrescription(item.prescriptionId)"
      >
        查看处方
      </van-button>
      <van-button
        type="primary"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
      >
        继续沟通
      </van-button>
    </div>
    <div class="foot" v-if="item.status === OrderType.ConsultComplete">
      <!-- <div class="more">
        <van-popover
          placement="top-start"
          v-model:show="showPopover"
          :actions="actions"
          @select="onSelect"
        >
          <template #reference> 更多 </template>
        </van-popover>
      </div> -->
      <consult-more :item="item" />
      <van-button
        class="gray"
        plain
        size="small"
        round
        :to="`/room?orderId=${item.id}`"
      >
        问诊记录
      </van-button>
      <van-button
        v-if="!item.evaluateId"
        type="primary"
        plain
        size="small"
        round
      >
        去评价
      </van-button>
      <van-button v-else class="gray" plain size="small" round>
        查看评价
      </van-button>
    </div>
    <div class="foot" v-if="item.status === OrderType.ConsultCancel">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="loadingOfDel"
        @click="delOrder(item)"
        >删除订单</van-button
      >
      <van-button type="primary" plain size="small" round to="/"
        >咨询其他医生</van-button
      >
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-item {
  border-radius: 4px;
  box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
  background-color: #fff;
  margin-bottom: 10px;
  .head {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 15px;
    .img {
      width: 20px;
      height: 20px;
    }
    > p {
      flex: 1;
      font-size: 15px;
      padding-left: 10px;
    }
    > span {
      color: var(--cp-tag);
      &.orange {
        color: #f2994a;
      }
      &.green {
        color: var(--cp-primary);
      }
    }
  }
  .body {
    padding: 15px 15px 8px 15px;
    .body-row {
      display: flex;
      margin-bottom: 7px;
    }
    .body-label {
      width: 62px;
      font-size: 13px;
      color: var(--cp-tip);
    }
    .body-value {
      width: 250px;
      &.tip {
        color: var(--cp-tip);
      }
    }
  }
  .foot {
    padding: 0 15px 15px 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .van-button {
      margin-left: 10px;
      padding: 0 16px;
      &.gray {
        color: var(--cp-text3);
        background-color: var(--cp-bg);
      }
    }
    .more {
      color: var(--cp-tag);
      flex: 1;
      font-size: 13px;
    }
  }
}
</style>
